﻿using System.Collections;
using System.IO;
using UnityEngine;

/// <summary>APK管理</summary>
public class ApkManager : MonoBehaviour
{

    private string m_AppPath;
    private AndroidJavaClass m_JavaClass;

    /**
     * 启动的时候就将 StreamingAssets/GooglePlayServicesforAR.apk 拷贝到应用 Application.persistentDataPath 目录下
     */
    private void Start()
    {
        m_AppPath = Application.persistentDataPath + "/GooglePlayServicesforAR.apk";
        m_JavaClass = new AndroidJavaClass("com.daliang.apkmanager.MainActivity");
        StartCoroutine(copyFile());
    }


    /// <summary>拷贝文件</summary>
    private IEnumerator copyFile()
    {
        FileInfo fileInfo = new FileInfo(m_AppPath);
        if (!fileInfo.Exists)
        {
            WWW w = new WWW(Application.streamingAssetsPath + "/GooglePlayServicesforAR.apk"); //arcore-1-22-203220563.apk
            yield return w;
            FileStream fs = fileInfo.Create();
            fs.Write(w.bytes, 0, w.bytes.Length);
            fs.Close();
            fs.Dispose();
        }
        else
        {
            LogUtils.debug("GooglePlayServicesforAR.apk 已经存在，不再拷贝");
        }
    }

    /// <summary>安装APK</summary>
    public void InstallAPK()
    {
        LogUtils.debug(m_JavaClass + "");
        LogUtils.debug(m_AppPath);
        bool b = m_JavaClass.CallStatic<bool>("installAPK", m_AppPath);
        LogUtils.debug("是否安装成功：" + b);
    }

}
